package com.ngkj.zmfptback.business.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ngkj.zmfptCommon.common.bean.CommonReslut;
import com.ngkj.zmfptCommon.common.util.ResultEnum;
import com.ngkj.zmfptback.business.bean.RentalRequestEntiry;
import com.ngkj.zmfptback.business.bean.RentalResponseEntity;
import com.ngkj.zmfptback.business.service.read.RentalReadService;
import com.ngkj.zmfptback.business.service.write.RentalWriteService;
import com.ngkj.zmfptback.sys.http.OkHttpCli;
import com.ngkj.zmfptback.sys.service.read.UserReadService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author ChenHN
 * @since 2021/12/31 creation
 **/
@RestController
public class RentalController {

    @Resource
    private RentalReadService rentalReadService;

    @Resource
    private RentalWriteService rentalWriteService;

    @Resource
    private UserReadService userReadService;


    @Resource
    private OkHttpCli okHttpCli;




    @GetMapping("/rental/getNoProcessedRentalList")
    public CommonReslut getNoProcessedRentalList(int pageNo, int pageSize) {
        try {
            Page<RentalResponseEntity> page = new Page<>(pageNo, pageSize);
            IPage<RentalResponseEntity> noProcessedRentalList = rentalReadService.getProcessedRentalList(false, page);
            return new CommonReslut(ResultEnum.ok, noProcessedRentalList);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }


    /**
     * 请求 ES 获取到信息
     *
     * @param rentalInfo
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getRentalList")
    public CommonReslut getRentalList(RentalRequestEntiry rentalInfo, int pageNo, int pageSize) {
        try {
            String url = "http://127.0.0.1:8002/searchRentalInfo";
            Map<String, String> map = new HashMap<>();
            map.put("xqxx.xqmc", rentalInfo.getXqmc());
            map.put("sqxx.sqmc", rentalInfo.getSqmc());
            map.put("sfsp", rentalInfo.getSfsp());
            map.put("qywzid", rentalInfo.getQywzid());
            map.put("pageNo", Integer.toString(pageNo));
            map.put("pageSize", Integer.toString(pageSize));
            String s = okHttpCli.doGet(url, map);
            if (StringUtils.isEmpty(s)){
               s = okHttpCli.doGet(url, map);
            }
            CommonReslut<String> stringCommonReslut = new CommonReslut<>();
            CommonReslut commonReslut = JSON.parseObject(s, stringCommonReslut.getClass());
            if (!ObjectUtils.isEmpty(commonReslut)&&commonReslut.getCode()==200){
                String data = (String) commonReslut.getData();
                Map<String,Object> map1 = JSON.parseObject(data, Map.class);
                return new CommonReslut(ResultEnum.ok, map1);
            }else{
                return new CommonReslut(ResultEnum.error);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }

    }

    @GetMapping("/rental/getProcessedRentalList")
    public CommonReslut getProcessedRentalList(int pageNo, int pageSize) {
        try {
            Page<RentalResponseEntity> page = new Page<>(pageNo, pageSize);
            IPage<RentalResponseEntity> noProcessedRentalList = rentalReadService.getProcessedRentalList(true, page);
            return new CommonReslut(ResultEnum.ok, noProcessedRentalList);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    /**
     * 同意审批该房源
     * @param agreeIds
     * @return
     */
    @PostMapping("/rental/agreeRentals")
    public CommonReslut agreeRentals(String agreeIds) {
        try {
            String[] agreeId = agreeIds.split(";");
            List<String> list = Arrays.asList(agreeId);
            rentalWriteService.agreeRentals(list);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/rental/agreeAllRental")
    public CommonReslut agreeAllRental() {
        try {
            rentalWriteService.agreeAllRental();
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    @PostMapping("/rental/refusedRentals")
    public CommonReslut refusedRentals(String refusedIds) {
        try {
            String[] refusedId = refusedIds.split(";");
            List<String> list = Arrays.asList(refusedId);
            rentalWriteService.refusedRentals(list);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    /**
     *发布房源
     * @param rentalRequestEntiry
     * @param files
     * @return
     */
    @PostMapping("/rental/addRentalInfo")
    public CommonReslut addRentalInfo(RentalRequestEntiry rentalRequestEntiry, MultipartFile[] files) {
        try {
            //获取真实信息id
            Long fdid = userReadService.getRealUserIdByUserId(rentalRequestEntiry.getFdid());
            rentalRequestEntiry.setFdid(fdid);
            //存储基本信息
            rentalWriteService.addRentalInfo(rentalRequestEntiry,files );
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }
    }

    /**
     * 取消发布 租房信息
     * @param czfxxlsh
     * @param userId
     * @return
     */
    @PostMapping("/rental/cancelRental")
    public CommonReslut cancelRental(Long czfxxlsh, Long userId) {
        try {
            Long fdid = rentalReadService.getfdIdByCzfxxlsh(czfxxlsh);
            Long realUserIdByUserId = userReadService.getRealUserIdByUserId(userId);
            //判断userId 与房屋id的房东是否为同一个人
            if (ObjectUtils.isEmpty(realUserIdByUserId) || fdid.longValue() != realUserIdByUserId.longValue()) {
                return new CommonReslut(ResultEnum.error);
            }
            rentalWriteService.cancelRental(czfxxlsh);
            return new CommonReslut(ResultEnum.ok);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonReslut(ResultEnum.error);
        }

    }


}
